package top.wilsonlv.jaguar.cloud.upms.feign;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.wilsonlv.jaguar.cloud.upms.entity.Device;
import top.wilsonlv.jaguar.cloud.upms.sdk.dto.DeviceBindDTO;
import top.wilsonlv.jaguar.cloud.upms.sdk.feign.RemoteDeviceService;
import top.wilsonlv.jaguar.cloud.upms.sdk.vo.DeviceVO;
import top.wilsonlv.jaguar.cloud.upms.service.DeviceService;
import top.wilsonlv.jaguar.commons.web.response.JsonResult;

import javax.validation.Valid;

/**
 * @author lvws
 * @since 2022/3/29 0029
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/feign/device")
public class RemoteDeviceServiceImpl implements RemoteDeviceService {

    private final DeviceService deviceService;

    @Override
    @GetMapping("/loadDevice")
    public JsonResult<DeviceVO> loadDevice(@RequestParam String deviceUid) {
        Device device = deviceService.getByDeviceUid(deviceUid);
        if (device == null) {
            return JsonResult.success(null);
        }
        return JsonResult.success(device.toVo(DeviceVO.class));
    }

    @Override
    @PostMapping("/bindDevice")
    public JsonResult<Void> bindDevice(@RequestParam Long userId,
                                       @RequestParam String deviceIp,
                                       @Valid @RequestBody DeviceBindDTO bindDTO) {
        deviceService.bindDevice(userId, deviceIp, bindDTO);
        return JsonResult.success();
    }

}
